﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EggTimer.Parsers
{
    public static class TimeParser
    {
        public static int getHours(string value)
        {
            return getColonSeparatedTime(value, TimeUnit.Hours);
        }

        public static int getMinutes(string value)
        {
            return getColonSeparatedTime(value, TimeUnit.Minutes);
        }

        public static int getSeconds(string value)
        {
            return getColonSeparatedTime(value, TimeUnit.Seconds);
        }

        private static int getColonSeparatedTime(string value, TimeUnit t)
        {
            if (value != null && value.Contains(':'))
            {
                string[] values = value.Split(':');
                switch (t)
                {
                    case TimeUnit.Hours:
                        if (values[0].ToLower().Contains('h'))
                            return Convert.ToInt32(values[0].ToLower().Replace("h", ""));
                        else
                            return Convert.ToInt32(values[0]);
                    case TimeUnit.Minutes:
                        if (values[1].ToLower().Contains('m'))
                            return Convert.ToInt32(values[1].ToLower().Replace("m", ""));
                        else
                            return Convert.ToInt32(values[1]);
                    case TimeUnit.Seconds:
                        if (values[2].ToLower().Contains('s'))
                            return Convert.ToInt32(values[2].ToLower().Replace("s", ""));
                        else
                            return Convert.ToInt32(values[2]);
                }
            }
            else
                throw new InvalidTimeFormat("The passed time is not the valid format. String: " + value + " Valid Format: 00h:00m:00s");
            return 0;
        }
    }

    public enum TimeUnit
    {
        Hours,
        Minutes,
        Seconds
    }
}
